﻿using AbpEntityFrameworkcore.DbContext;
using AbpToDomain;
using AbpToDomain.EntityInfo;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Volo.Abp.Modularity;

namespace AbpEntityFrameworkcore
{

    [DependsOn(
        typeof(AbpEntityFrameworkCoreModule),
        typeof(AbpToDomainModule)           
            )]
    public class AbpEntityFrameworkcoreModule:AbpModule
    {
       
        public override void ConfigureServices(ServiceConfigurationContext context)
        {

            string Lmsconnstr = Environment.GetEnvironmentVariable("珠海博世Re", EnvironmentVariableTarget.Machine);
            string Customconnstr = Environment.GetEnvironmentVariable("珠海博世Re_Custom", EnvironmentVariableTarget.Machine);

       
         
            Configure<AbpDbContextOptions>(opt =>
            {
                opt.UseSqlServer();//使用sql           
            });


            //配置wms数据库连接
            context.Services.AddAbpDbContext<MyDbContext>(opt =>
            {
                opt.AddDefaultRepositories(true);//打开仓储           
            });
            Configure<AbpDbConnectionOptions>(opt =>
            {

                opt.ConnectionStrings["Lmsconnstr"] = Lmsconnstr;//配置连接字符串

            });


            //配置客户数据库连接
            context.Services.AddAbpDbContext<CustomDbContext>(opt =>
            {
                opt.AddDefaultRepositories(true);//打开仓储    
            });
            Configure<AbpDbConnectionOptions>(opt =>
            {

                opt.ConnectionStrings["Customconnstr"] = Customconnstr;//配置连接字符串

            });





            Configure<AbpEntityOptions>(options =>
            {
                options.Entity<Box>(orderOptions =>
                {
                    orderOptions.DefaultWithDetailsFunc = query => query.Include(o => o.WarehouseLine).Include(o=>o.BoxGrids).ThenInclude(x=>x.BoxPositions);
              
                });
                options.Entity<BoxGrid>(orderOptions =>
                {
                    orderOptions.DefaultWithDetailsFunc = query => query.Include(o => o.BoxPositions).Include(o=>o.TheBox);
                });
                options.Entity<MaterialBase>(options =>
                {
                    options.DefaultWithDetailsFunc = query => query.Include(x=>x.BoxPosition).ThenInclude(x=>x.TheBoxGrid). ThenInclude(x=>x.TheBox).ThenInclude(x=>x.WarehouseLine);
                });
                options.Entity<Warehouse>(options =>
                {
                    options.DefaultWithDetailsFunc = query => query.Include(o => o.warehouselines);
                });
                options.Entity<TaskOrder>(options =>
                {
                    options.DefaultWithDetailsFunc = query => query.Include(o => o.Box);
                });
                options.Entity<NeetOutMaterial>(options =>
                {
                    options.DefaultWithDetailsFunc = query => query.Include(o => o.Material);
                });
                options.Entity<BoxPosition>(options =>
                {
                    options.DefaultWithDetailsFunc = query => query.Include(o => o.MaterialBase);
                });

            });


        }

      
    }
}
